<template>
	<view class="">
		<u-upload ref="uploadRef" :max-count="count" width="100" height="100" upload-text="" name="files"
			:action="action" @afterRead="afterRead" :header="header" :file-list="defaultShow" @on-success="onSuccess"
			@on-change="onChange" del-icon="setting" del-bg-color="#fff" del-color="rgb(71 76 76)"
			@on-choose-complete="onChooseComplete" @on-error="onError" :deletable="false" chanableBgColor="#fff"
			chanableIcon="setting" chanableColor="#000" @on-choose-fail="onChooseFail"></u-upload>
	</view>
</template>

<script>
	import {
		baseURL
	} from '@/common/request';
	import {
		getToken
	} from '@/common/user';
	import {
		setLoadingToast,
		setToast
	} from '@/common/utils';
	export default {
		name: "upload",
		props: {
			defaultShow: {
				type: Array,
				default: () => []
			},
			keyName: {
				type: String,
				default: () => []
			},
			count: {
				type: Number,
				default: () => 1
			},
		},
		data() {
			return {
				header: {
					'Authorization': getToken()
				},
				action: baseURL + 'common/uploadS3Files',
				state: false
			};
		},
		methods: {
			// 清空文件列表
			clear() {
				this.$refs.uploadRef.clear()
			},
			// 图片上传后，无论成功或者失败都会触发
			onChange(eee) {
				uni.hideLoading()
			},
			// 上传成功
			onSuccess(data) {
				this.$emit("success", {
					[this.keyName]: data.fileName
				})
				// 上传成功 清空回显的数据
				this.state = true
				this.localEaco = []
				setTimeout(() => {
					setToast('上传成功')
				}, 200)
			},
			// 选择图片
			onChooseComplete(e) {
				console.log('e ================xsxs==> ', e);
				setLoadingToast('上传中...')
			},
			// 上传失败
			onError(err) {
				console.log('err ====> ', err);
				uni.hideLoading()
			},
			// 
			uploadClick() {
				console.log('c===> uploadClick');
			},
		}
	}
</script>

<style>

</style>